<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\model\Activity;
use apps\marriage\core\service\ActivityMemberService;
use apps\marriage\core\service\ActivityService;
use think\Exception;
use think\Request;

class ActivityController extends BaseMarriageMpController
{
    /**
     * @var ActivityService
     */
    private $activityService;

    /**
     * @var ActivityMemberService
     */
    private $activityMemberService;

    protected function init()
    {
        parent::init();
        $this->activityService = service('Activity', SERVICE_NAMESPACE);
        $this->activityMemberService = service('ActivityMember', SERVICE_NAMESPACE);
    }

    public function info(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $activity = $this->activityService->getByPk($id);
        if(empty($activity)) {
            throw new Exception('活动不存在');
        }
        $activity = $this->activityService->format($activity);
        $joined = false;
        if($this->user && $this->activityMemberService->info([
                'user_id' => $this->user['id'],
                'activity_id' => $id
            ])) {
            $joined = true;
        }
        return $this->success([
            'activity' => $activity,
            'joined' => $joined
        ]);
    }

    public function sign_in(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $activity = $this->activityService->getByPk($id);
        if(empty($activity)) {
            throw new Exception('活动不存在');
        }
        $activity = $this->activityService->format($activity);
        if($activity['end_time'] < time()) {
            throw new Exception('活动已结束');
        }
        $joined = false;
        if($this->activityMemberService->info([
                'user_id' => $this->user['id'],
                'activity_id' => $id
            ])) {
            $joined = true;
        }
        return $this->success([
            'activity' => $activity,
            'joined' => $joined
        ]);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'status' => Activity::STATUS_ONLINE
        ];
        $res = $this->activityService->pageListByParams($params, $pageSize);
        if($res['data']) {
            foreach ($res['data'] as &$rs) {
                $rs = $this->activityService->format($rs);
            }
        }
        return $this->success($res);
    }

    public function apply(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        try {
            $this->activityService->apply($id, $this->user['id']);
            return $this->success([
                'status' => 'success',
                'msg' => ''
            ]);
        } catch (Exception $e) {
            if($e->getCode() == ERROR_ACTIVITY_APPLY_WAIT_PAY) {
                return $this->success([
                    'status' => 'wait_pay',
                    'msg' => '请先支付活动费用'
                ]);
            } else {
                throw new Exception($e->getMessage());
            }
        }
    }

    public function my(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'user_id' => $this->user['id']
        ];
        $res = $this->activityMemberService->pageListByParams($params, $pageSize);
        if($res['data']) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $activity = $this->activityService->getByPk($rs['activity_id']);
                $activity = $this->activityService->format($activity);
                $dataList[] = [
                    'activity' => $activity,
                    'apply_time' => $rs['apply_time']
                ];
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }

    public function share_poster(Request $request)
    {
        $activityId = $request->param('activity_id');
        $fromuid = $request->param('fromuid');
        $url = $this->activityService->getSharePoster($activityId, $fromuid);
        return $this->success([
            'url' => $url
        ]);
    }
}